import API from '~/utils/request'

export const BASE_URL = env.isDev
  ? import.meta.env.VITE_API_URL_DEV
  : import.meta.env.VITE_API_URL_PROD

export const api = new API({
  baseURL: BASE_URL,
  successCode: '200',
  headers: {
    'Accept': '*/*',
    'Content-Type': 'application/json; charset=utf-8',
    'Authorization':
      'Bearer ' +
      'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEwMDM1LCJjcmVhdGVUaW1lIjoxNzI5MzIwMzI5fQ.Smo0U8IHpoORhQ6NJm8si1r5Zx0wD6P7kpJ3cQkkiqc',
  },
  appendData: (config: any) => {
    console.log('config=>', config)
    // config.params._t = +new Date()
  },
})

interface AliResponseData {
  task_id: string
  result: string
  status: number
  message: string
}

class ALI_API extends API {
  action(config: any) {
    return new Promise((resolve, reject) => {
      super
        .action(config)
        .then((res) => {
          const data = res.data as AliResponseData
          resolve(data)
        })
        .catch((e) => {
          reject(e)
        })
    })
  }
}

// 后台以外的场景使用，比如阿里云api等
export const aliApi = new ALI_API({
  baseURL: BASE_URL,
  returnResponse: true,
})
